//
//  MeatOrderViewController.m
//  PigMarket
//
//  Created by 张敬 on 2019/7/24.
//  Copyright © 2019年 pigmarket.oc. All rights reserved.
//

#import "MeatOrderViewController.h"
#import "MeatPayViewController.h"

#import "MeatOrderListCell.h"
#import "MeatOrderLocationCell.h"
#import "MeatPayView.h"
#import "MeatTipLabel.h"

#import "MeatOrderBox.h"

@interface MeatOrderViewController ()


/**
 tap
 */
@property (nonatomic, strong) UITapGestureRecognizer *tap;


/**
 当前是第几个section
 */
@property (nonatomic, assign) NSInteger currentIndex;


/**
 提示标签
 */
@property (nonatomic, strong) MeatTipLabel *tipLabel;


/**
 footer
 */
@property (nonatomic, strong) MeatPayView *footer;



@end

@implementation MeatOrderViewController

#pragma mark - lifecycle methods

- (void)setupUI
{
    
    self.currentIndex = 0;

    {
        self.baseNavView.navTitleLabel.text = @"确认订单";
    }
    
    {
        [self.view addSubview:self.tipLabel];
        [self.view addSubview:self.footer];
    }
    
    
    [self addkeyBoardNotification];
    
}


- (void)setupLayout
{
    _footer.mj_y = KScreenHeight - _footer.height;
    _tipLabel.mj_y = _footer.top - _tipLabel.height;
    
    {
        self.tableView.mj_y = self.baseNavView.bottom;
        self.tableView.height = _tipLabel.top  - 5 - TJ_NavBarHeight;
    }
}

- (void)initData{
    
}

#pragma mark - networking methods
#pragma mark - response methods
#pragma mark - public methods
#pragma mark - tableview delegate/dataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 5;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section ==0) {
        return 90;
    }
    return 183;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *sectionView = [[UIView alloc] init];
    sectionView.backgroundColor = mBackgroudColor;
    return sectionView;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        
        MeatOrderLocationCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MeatOrderLocationCell"];
        cell.type = MeatOrderLocationCellType_Have;
        return cell;
    }
    
    
    MeatOrderListCell *newsCell = [tableView dequeueReusableCellWithIdentifier:@"MeatOrderListCell"];
    newsCell.shopName = @"行走的铺子";
    newsCell.type = indexPath.section == 2 ?MeatOrderListCellType_One :MeatOrderListCellType_More;
    
    kWeakSelf(self);
    newsCell.editBlock = ^(NSInteger params) {
        weakself.currentIndex = indexPath.section;
    };

    return newsCell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    MeatOrderBox *box = [[MeatOrderBox alloc] init];
    [box showView];
    
}


#pragma mark - setter and getter

- (MeatTipLabel *)tipLabel{
    if (!_tipLabel) {
        _tipLabel = [[MeatTipLabel alloc] init];
        _tipLabel.height = 34;
        
        NSString *contentStr = @"为保障您的财产安全，等小猪主人付清饲养费，预定单再付不迟哦！";
        _tipLabel.text = contentStr;
        [_tipLabel sizeToFit];
    }
    return _tipLabel;
}


- (MeatPayView *)footer{
    if (!_footer) {
        _footer = [[MeatPayView alloc] init];
        _footer.type = MeatPayType_Submit;
        
        //全选Block
        @weakify(self);
        //支付Block
        _footer.payCartBlock = ^{
            @strongify(self);
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"配送范围限制" message:@"选择该地址可能需要重新挑选商品" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            }];
            [cancelAction setValue:UIColorFromRGB(0x363636) forKey:@"titleTextColor"];
            
            UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
                
                MeatPayViewController *vc = [[MeatPayViewController alloc] init];
                [self.navigationController pushViewController:vc animated:YES];
            }];
            [okAction setValue:mMainColor forKey:@"titleTextColor"];
            
            
            [alert addAction:cancelAction];
            [alert addAction:okAction];
            [self presentViewController:alert animated:YES completion:nil];

        };
        
    }
    return _footer;
}


#pragma mark - override methods

- (void)registerTableViewCell{
    
    [self.tableView registerClass:[MeatOrderListCell class] forCellReuseIdentifier:@"MeatOrderListCell"];
    [self.tableView registerClass:[MeatOrderLocationCell class] forCellReuseIdentifier:@"MeatOrderLocationCell"];

    self.tableView.height = KScreenHeight - (TJ_NavBarHeight);

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 10)];
    view.backgroundColor = mBackgroudColor;
    self.tableView.tableFooterView = view;
}


#pragma mark - other methods

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    [self.view endEditing:YES];
    CGFloat sectionHeaderHeight = 10;
    if(scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
    } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
    }
}


#pragma mark - notification methods

- (void)textFieldTextChanged:(NSNotification *)notification
{
    UITextField *textField = notification.object;
    
    NSLog( @"______ 第 %ld 组  %@",  self.currentIndex, textField.text);
   // self.model.phone = textField.text;
}

- (void)keyboardWillShow:(NSNotification *)notification{
    NSDictionary *info = [notification userInfo];
    CGFloat duration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    
    kWeakSelf(self);
    [UIView animateWithDuration:duration animations:^{
        weakself.tableView.mj_y = 0;
    }];
    
    //当键盘升起的时候添加点击降落操作
    if (!self.tap) {
        self.tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
    }
    [self.view addGestureRecognizer:self.tap];
}

//取消键盘响应
- (void)hideKeyboard{
    
    [self.view endEditing:YES];
}


- (void)keyboardWillHide:(NSNotification *)notification{
    
    self.tableView.mj_y = 0;
    if (self.tap) {
        [self.view removeGestureRecognizer:self.tap];
    }
}


//添加键盘通知
- (void)addkeyBoardNotification{
    
    [kNotificationCenter addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [kNotificationCenter addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    [kNotificationCenter addObserver:self selector:@selector(textFieldTextChanged:) name:UITextFieldTextDidChangeNotification object:nil];
}

//移除键盘通知
- (void)removeKeyboardNotification{
    
    [kNotificationCenter removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [kNotificationCenter removeObserver:self name:UIKeyboardWillHideNotification object:nil];
    [kNotificationCenter removeObserver:self name:UITextFieldTextDidChangeNotification object:nil];
}

- (void)dealloc{
    
    [self removeKeyboardNotification];
}



@end
